//
//  EmployeeInfoV.swift
//  Employee
//
//  Created by swordjoy on 2017/3/30.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class EmployeeInfoV: UIView {

    var headImage: UIImage? = UIImage() {
        didSet {
            guard let temp = headImage else {
                headIconImageV.image = GlobalConfig.DefaultHeadIconImage
                return
            }
            
            headIconImageV.image = temp
        }
    }
  
  var headIconClickClosure: ( (UIImageView) -> () )?
  
  var editHeadIconClosure: VoidClosure?
  
  fileprivate var contentV: UIView!

  fileprivate var headIconImageV: UIImageView!
  fileprivate var editB: UIButton!
  fileprivate var nicknameLabel: UILabel!
  fileprivate var phoneNumberLabel: UILabel!
  fileprivate var departmentLabel: UILabel!
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor.clear
    
    create()
    update()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

// MARK: - interface


extension EmployeeInfoV {
  
  func update() {
    if let data = User.headImageData {
        headIconImageV.image = UIImage(data: data)
    }else {
         headIconImageV.setImage(with: User.avatar, placeholder: GlobalConfig.DefaultHeadIconImage)
    }
    
    nicknameLabel.text = User.name
    phoneNumberLabel.text = User.phone
    departmentLabel.text = User.organization
  }
  
}

// MARK: - action

extension EmployeeInfoV {
  
  @objc fileprivate func editAction() {
    
    guard let closure = editHeadIconClosure else { return }
    closure()
    
  }
  
  @objc fileprivate func headIconClickAction() {
    
    guard let closure = headIconClickClosure else { return }
    closure(headIconImageV)
    
  }
  
}

// MARK: - fileprivate

extension EmployeeInfoV {
  
  fileprivate func create() {
    
    contentV = UIView(frame: bounds)
    addSubview(contentV)
    contentV.backgroundColor = GlobalConfig.BGSkyBlueColor
  
    headIconImageV = UIImageView()
    contentV.addSubview(headIconImageV)
    headIconImageV.setImage(with: User.avatar, placeholder: GlobalConfig.PlaceholderImage)
    headIconImageV.snp.makeConstraints { (make) in
      make.centerX.equalToSuperview()
      make.top.equalTo(30)
      make.width.height.equalTo(80)
    }
    headIconImageV.layer.cornerRadius = 4
    headIconImageV.clipsToBounds = true
    
    headIconImageV.isUserInteractionEnabled = true
    let gesture = UITapGestureRecognizer(target: self, action: #selector(headIconClickAction))
    headIconImageV.addGestureRecognizer(gesture)
    
    editB = UIButton()
    contentV.addSubview(editB)
    let image = UIImage(named: "head-edit-icon")
    editB.setImage(image, for: .normal)
    editB.addTarget(self, action: #selector(editAction), for: .touchUpInside)
    editB.snp.makeConstraints { (make) in
      make.centerX.equalTo(headIconImageV.snp.right)
      make.centerY.equalTo(headIconImageV.snp.top)
      make.height.width.equalTo(44)
    }
    
    nicknameLabel = UILabel()
    contentV.addSubview(nicknameLabel)
    nicknameLabel.font = GlobalConfig.system22Font
    nicknameLabel.textColor = UIColor.white
    nicknameLabel.snp.makeConstraints { (make) in
      make.centerX.equalToSuperview()
      make.top.equalTo(headIconImageV.snp.bottom).offset(25)
    }
    
    phoneNumberLabel = UILabel()
    contentV.addSubview(phoneNumberLabel)
    phoneNumberLabel.font = GlobalConfig.system18Font
    phoneNumberLabel.textColor = UIColor.white
    phoneNumberLabel.snp.makeConstraints { (make) in
      make.centerX.equalToSuperview()
      make.top.equalTo(nicknameLabel.snp.bottom).offset(13)
    }
    
    departmentLabel = UILabel()
    contentV.addSubview(departmentLabel)
    departmentLabel.font = GlobalConfig.system14Font
//    departmentLabel.textColor = GlobalConfig.TextLightColor
    departmentLabel.textColor = UIColor.hex("#fefeff", alpha: 0.5)
    departmentLabel.snp.makeConstraints { (make) in
      make.centerX.equalToSuperview()
      make.top.equalTo(phoneNumberLabel.snp.bottom).offset(13)
    }
    
  }
}
